package com.wyzz.hr.action;

import javax.servlet.http.HttpSession;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.UserBo;
import com.wyzz.hr.bo.impl.EmpDistinctNo;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.spring.SpringBeanFactory;

public class ModifyUserStatus extends BaseAction {

	public ModifyUserStatus() {
	}

	public int updateUserStatus(String userId, int userStatus,
			HttpSession session) {
		String flt = DWRUtil.checkAuth("userList", "execute");
		if ("error".equalsIgnoreCase(flt))
			return 4;
		if ("demo".equals(session.getAttribute("loginModel"))
				&& userId.length() >= 5
				&& "TYHRM".equalsIgnoreCase(EmpDistinctNo.getEmpDistinctNo(
						userId).substring(0, 5)))
			return 6;
		UserBo userBo;
		Userinfo user;
		userBo = (UserBo) SpringBeanFactory.getBean("userService");
		user = userBo.getUserById(userId);
		if (user == null)
			return 5;
		Userinfo userinfo = (Userinfo) session.getAttribute("userinfo");
		if (user.getId() != null && userinfo.getId().equals(user.getId()))
			return 2;
		Userinfo oldUserinfo = userBo.getUserById(user.getId());
		if (userBo.checkAuthModule(oldUserinfo, "911")
				&& userBo.getAdminCount("911") < 2)
			return 3;
		Employee currentEmployee = ((Userinfo) session.getAttribute("userinfo"))
				.getEmployee();
		String createrId = currentEmployee.getId();
		if (userBo.updateStatus(userId, userStatus, createrId))
			return userStatus;
		try {
			return 5;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 5;
	}

	private static final long serialVersionUID = 0xc53a77b0ae03eebfL;
	private static final String AUTHMODULE = "911";
}
